package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.BmsVolunteerRegistrationParam;
import com.macro.mall.model.BmsVolunteerRegistration;
import com.macro.mall.service.BmsVolunteeRegistrationService;
import com.macro.mall.utils.ExcelUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.net.URLEncoder;

/**
 * 报名信息人员表ontroller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "BmsVolunteeRegistrationController")
@Tag(name = "BmsVolunteeRegistrationController", description = "招募人员信息管理")
@RequestMapping("/voluntee")
public class BmsVolunteeRegistrationController {
    @Autowired
    private BmsVolunteeRegistrationService bmsVolunteeRegistrationService;


    @ApiOperation("分页查询招募信息")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BmsVolunteerRegistration>> getList(@RequestBody BmsVolunteerRegistrationParam bmsVolunteerRegistrationParam) {
        List<BmsVolunteerRegistration> bmsVolunteerRegistrationList = bmsVolunteeRegistrationService.getList(bmsVolunteerRegistrationParam);
        return CommonResult.success(CommonPage.restPage(bmsVolunteerRegistrationList));
    }

    @ApiOperation("根据id获取招募人员信息信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BmsVolunteerRegistration> getItem(@PathVariable Long id) {
        BmsVolunteerRegistration bmsVolunteerRegistration = bmsVolunteeRegistrationService.getDetail(id);
        return CommonResult.success(bmsVolunteerRegistration);
    }
    
    @ApiOperation("文件导出")
    @RequestMapping(value = "/excel", method = RequestMethod.POST)
    public void excel(@RequestBody BmsVolunteerRegistrationParam bmsVolunteerRegistrationParam, HttpServletResponse response) throws Exception {
    	List<BmsVolunteerRegistration> list = bmsVolunteeRegistrationService.findList(bmsVolunteerRegistrationParam);
    	String fileName = URLEncoder.encode("志愿者报名数据.xlsx", "UTF-8");
    	response.setCharacterEncoding("utf-8");
    	ExcelUtils.write(response, fileName, "志愿者报名", list, BmsVolunteerRegistration.class);
    }
}
